<?php
header("Content-type: text/html; charset=utf-8");
//require ROOT_PATH.'protected/include/ext/phpQuery/phpQuery.php';
//require ROOT_PATH.'protected/apps/collection/controller/taoartController.php';
class indexController extends baseController{
	static protected $visitpath='';//图片访问路径
	static protected $filepath ="";

	public function __construct()
	{
		parent::__construct();
		$this->visitpath=config('URL_HTTP_PROTOCOL').$_SERVER['HTTP_HOST']."/upload/collectibles/";
		$this->filepath=ROOT_PATH."upload/collectibles";
	}


	// $pagepage 页码页 $listpage 列表页 $detailspage 详情页
	public function index(){
        set_time_limit(60*60*22);
	    cpError::write('开始新一轮抓取任务');
        /*//弥补处理展品,应该就用不着了
        $exhibits = new exhibitsMakerController();
        $exhibits->index();die;*/



        //自动获取城市省份id
        /*$common = new commonController();
        $common->changetable();die;*/

        /*$yachang = new yachangController();
        $yachang->getContent();die;*/

        //打开后扫描国家博物馆，只第一次打开，往后不要打开，以免造成数据丢失
        /*$excel = new phpexcelController();
        $excel->run();die;*/

        /*//获取坐标测试
        $map = new mapController();
        $data = $map->getlocation('故宫博物院','北京');
        print_r($data);
        die;*/


        /*//测试获取维基百科,暂不可用
        $wiki = new wikiController();
        $wiki->getcontents('故宫博物院');
        die;*/



        //打开后扫描画廊    ok  天天开
       /* $gallery = new gallerycomehereController();
        $gallery->run();


        //打开后扫描艺术家，只打开一次，往后不要打开   ok ,天天开
        $artist = new artistcomehereController();
        $artist->run();*/
        //die;



        /*$haha = parse_url('http://exhibit.meishujia.cn//?act=app&appid=4099&mid=15375&p=view');
        print_r($haha);die;*/


        //yachang
        $yachang = new yachangController();
        $yachang->getContent();
        //die;




        //淘艺术
        $taoart = new taoartController();
        $taoart->getContent();
        //die;


        //打开后处理museum表，根据百科走，未来百科不断更新，继续打开，或许可以得到新数据
        $museum = new museumMakerController();
        $museum->makedetailed();
        $museum->run();die;

/*        $str = '[北京]-北京市朝阳区百子湾路32号苹果社区4号楼-(今日美术馆)';

        $ret = $this->citymaker($str,123);
        print_r($ret);die;*/
        //


/*        $str = '西213安lhjsdjkf一段 中"陕fjsdgf西文-检-索——_测试';
        $ret = judgeCityAndProvince($str);
        print_r($ret);die;
        $arr = array();

        preg_match_all("/[\x{4e00}-\x{9fa5}]+/u",$str,$arr);
        print_r($arr[0]);die;
        echo date(Y)+1;die;

        $temp_date = '2011/01/05';
        $temp_date_haha = '01/06';
        if($temp_date < $temp_date_haha){
            echo $temp_date_haha;
        }else{
            echo $temp_date;
        }
        //echo date('Y-m-d',strtotime($temp_date));


        die;*/
        //	$fieldlist = model('extend')->select("pid='{$extendid}'","","norder DESC");
        //http://exhibit.meishujia.cn/index.php?act=app&appid=4099&btm=~now&etm=9999999999999 $pagepage[0]['mainurl']

        $pagepage = $this->getPagePage();

        //对页码页进行处理
        $listnum = 0;
        while(isset($pagepage[$listnum])){
            $url = $pagepage[$listnum]['mainurl'];

            if(($ret = url_crawler($url)) == false){
               ++$listnum;
                continue;
            }
            //echo '抓取没问题';die;
            $page = $this->getPageNumber($ret,$pagepage[$listnum]);
            if($page == false){
                ++$listnum;
                continue;
            }

            //获取列表页和详情页解析规则
            $listpage = $this->getListPage($pagepage[$listnum]['gathererid'])[0];
            $detailspage = $this->getDetailsPage($pagepage[$listnum]['gathererid'])[0];

            //print_r($listpage);die;
            $nowpage = 0;
            while($nowpage <= $page){

                $url = $pagepage[$listnum]['pageurl_perfix'].$nowpage.$pagepage[$listnum]['pageurl_postfix'];

                /*echo '<br />'.$url.'<br />';
                ++$nowpage;
                continue;*/

                $urllist = $this->getListPageUrl($url,$listpage);

                for($i = 0;isset($urllist[$i]);$i++){
                    if(($contents = url_crawler($urllist[$i])) == false){
                        ++$listnum;
                        continue;
                    }

                    //$detailsInfo 存储所有需要存储的数据
                    $detailsandimg = $this->getDetails($contents,$detailspage,$urllist[$i]);
                    $detailsInfo = $detailsandimg['details_list'];
                    $imgInfo = $detailsandimg['img_list'];
                    //print_r($detailsInfo);die;

                    //接下来直接进行存储
                    if(is_array($detailsInfo)&& $detailsInfo['name']!=''){
                        $findbyname=model('maexhibitions')->find("name='".$detailsInfo['name']."'");
                        if(!$findbyname){
                            $detailsInfo['visittime'] = time();
                            $detailsInfo['url'] = $urllist[$i];
                            $id = model('maexhibitions')->saveData($detailsInfo);
                            //图片存储
                            $i = 0;
                            while(isset($imgInfo[$i])&&$id != ''){
                                $imgInfo[$i]['exhibitionsID'] = $id;
                                $imgpath = $this->saveImg($imgInfo[$i]['imgurl']);

                                if($imgpath != '') {
                                    $imgInfo[$i]['imgpath'] = 'exhibits/' . date('Ymd') . '/' . $imgpath;
                                }else{
                                    ++$i;
                                    continue;
                                }
                                model('maexhibits')->saveData($imgInfo[$i]);
                                ++$i;
                            }

                        }else{
                            cpError::write($findbyname,'数据已存在');
                            //代表数据已存在
                        }
                    }
                }
                ++$nowpage;
                //sleep(10);
            }
            ++$listnum;
        }
        //die;
        //各种小网站
        $handle = new HandlehtmlController();
        $handle->index();

        exit;
    }

    //读出所有需要扒取的url 读取页码页规则
    protected function getPagePage(){
        $list = model('gatherer')->getPagePage();
        //print_r($list);
        return $list;
    }
    //读取列表页规则
    protected function getListPage($id){
        $list = model('gatherer')->getListPage($id);
        return $list;
    }
    //读取详情页规则
    protected function getDetailsPage($id){
        $list = model('gatherer')->getDetailsPage($id);
        return $list;
    }
    //读详情页各项通用规则
    protected function getInit(){
        return model('gatherer')->getInit();
    }

    //抓取页码页的页码 处理PagePage  @return 该网站包含多少页展览
    protected function getPageNumber($contents,$pagepage){
        $page= 0;
        $fence_start = $pagepage['fence_start'];
        $fence_end = $pagepage['fence_end'];

        $pos = mb_strpos($contents,$fence_start);
        if($pos === false){
            cpError::write('url：'.$pagepage['mainurl'].'页码页fence_start标志错误','获取页码错误');
            return false;
        }
        $contents = mb_substr($contents,$pos+mb_strlen($fence_start));
        $pos_end = mb_strpos($contents,$fence_end);
        if($pos_end === false){
            $pos_end = mb_strpos($contents,'页');
            if($pos_end === false){
                $pos_end = mb_strpos($contents,' ');
                if($pos_end === false){
                    cpError::write('url:'.$pagepage['mainurl'].'页码页fence_end标志错误','获取页码错误');
                    return false;
                }
            }
        }
         $contents = mb_substr($contents,0,$pos_end);

        //页码片段得到，解析总页码
        $patterns = "/\d+/";
        preg_match_all($patterns,$contents,$page_arr);


        //print_r($page_arr);die;
        for($i = 0;isset($page_arr[0][$i]); $i++){
            if($page < intval($page_arr[0][$i])){
                $page = intval($page_arr[0][$i]);

            }
        }

        return $page;
    }

    //对整个列表页进行处理 处理ListPage @return 分析的得到的详情页url
    protected function getListPageUrl($url,$listpage){


        if(($contents = url_crawler($url)) == false){
            return false;
        }

        $fence_start = $listpage['fence_start'];
        $fence_end = $listpage['fence_end'];

        $pos = mb_strpos($contents,$fence_start);

        if($pos === false){
            cpError::write('url：'.$url.'列表页fence_start标志错误','获取列表错误');
            return false;
        }

        $contents = mb_substr($contents,$pos+mb_strlen($fence_start));
        $pos_end = mb_strpos($contents,$fence_end);
        if($pos_end === false){
            cpError::write('url：'.$url.'列表页fence_end标志错误','获取列表错误');
        }

        $contents = mb_substr($contents,0,$pos_end+mb_strlen($fence_end));

        $short_start = $listpage['short_start'];
        $short_end = $listpage['short_end'];

        $urllist = array();
        $urllist_num = 0;

        $pos = mb_strpos($contents,$short_start);
        if($pos === false){
            cpError::write('url：'.$url.'列表页short_start标志错误','获取列表片段错误');
        }
        while ($pos !== false){
            $contents = mb_substr($contents,$pos+mb_strlen($short_start));

            $pos_end = mb_strpos($contents,$short_end);

            //echo $pos_end;die;
            if($pos_end === false){
                cpError::write('url：'.$url.'列表页short_end标志错误','获取列表片段错误');
                break;
            }
            $temp_contents = mb_substr($contents,0,$pos_end);

            $url_temp = $this->getListPageUrlHref($temp_contents,$url);
            $temp_pos = mb_strpos($url_temp,$listpage['details_perfix']);
            if($temp_pos === false){
                $urllist[$urllist_num] = $listpage['details_perfix'].$url_temp;
            }else{
                $urllist[$urllist_num] = $url_temp;
            }

            //未开放功能，
 /*           //粗雕
            $detailsinfo_Probably = $this->getDetailsExInt($listpage,$temp_contents,1);
            //细啄
            if(($detailsinfo_contents = url_crawler($detailsinfo_Probably['url'])) == false){
                $pos = mb_strpos($contents,$short_start);
                ++$urllist_num;
                continue;
            }*/

            $pos = mb_strpos($contents,$short_start);
            ++$urllist_num;
        }

        return $urllist;
    }

    //列表页的片段中抓取url，$contents 片段
    protected function getListPageUrlHref($contents,$url =null){
        $pos = mb_strpos($contents,'href="');
        if($pos === false){
            cpError::write('url:'.$url.'列表页片段用href="无法解析，请速查看修改','获取列表片段错误');
            return false;
        }

        $contents = mb_substr($contents,$pos+mb_strlen('href="'));
        $pos = mb_strpos($contents,'"');
        if($pos === false){
            $pos = mb_strpos($contents,' ');
        }
        $url = mb_substr($contents,0,$pos);

        return $url;
    }

    //处理详情页 处理DetailsPage, $contents 待处理的详情页内容 $detailspage 待处理详情页的抓取规则
    /*
     * @return  array(
     *    '0' => array(
     *          '0'=>array(
     *              'imgurl' => '',
     *              'name'=> ''
     *              'imgpath' =>''
     *           )
     *      ),
     *    '1' => array(
     *          'type'=> '',
     *           ......
     *      ),
     * )
     *
     *
     */
    protected function getDetails($contents,$detailspage,$url = null){
        //获取整体标签，标签会有多种
        $fence_start = explode(';',$detailspage['fence_start']);
        $fence_end = $detailspage['fence_end'];

        $pos = mb_strpos($contents,$fence_start[0]);
        if($pos === false){
            $num_temp = 1;
        }else{
            $num_temp = 0;
        }

        while($pos === false){
            $pos = mb_strpos($contents,$fence_start[$num_temp]);
            ++$num_temp;
        }
        if($num_temp !=0) {
            --$num_temp;
        }


        if($pos === false){
            cpError::write('url:'.$url.'详情页fence_start找不到，请补全','详情页分析错误');
            return false;
        }
        $contents = mb_substr($contents,$pos+mb_strlen($fence_start[$num_temp]));


        $pos_end = mb_strpos($contents,$fence_end);

        if($pos_end === false){
            cpError::write('url:'.$url.'详情页fence_end找不到，请补全','详情页分析错误');
            return false;
        }
        $contents = mb_substr($contents,0,$pos_end+mb_strlen($fence_end));


        //抓取src图片信息，图片的url拼接方式页要存储，标签使用<img src="或去括号后使用<imgsrc="
        $img_list = $this->getImgUrl($contents,$detailspage,$url);


        //抓取除imgurl以外的其他信息
        $details_list = $this->getDetailsExImg($detailspage,$contents,$url);

        /*echo 'getDetails()=>$img_list:::';
        print_r($img_list);
        echo 'getDetails()=>$details_list[]:::';
        print_r($details_list);die;*/

        $ret['img_list'] = $img_list;
        $ret['details_list'] = $details_list;

        return $ret;
    }


    //抓取除introduction以外的其他数据
    protected function getDetailsExImg($detailspage,$contents,$url){
        $details_list = array();
        $currency = ($this->getInit())[0];

        //获取段落规则  $anything_end应该与$introduction_start相同
        $anything_start = explode(' ',$detailspage['anything_start']);
        $anything_end = explode(' ',$detailspage['anything_end']);

        $introduction_start = explode(' ',$detailspage['introduction_start']);
        $introduction_end = explode(' ',$detailspage['introduction_end']);

        //删除空格,通用规则
        $name_exhibition_start = explode(';',$currency['name_exhibition_start']);

        $time_exhibition_start = explode(';',$currency['time_exhibition_start']);

        $city_province_start = explode(';',$currency['city_province_start']);

        $address_start = explode(';',$currency['address_start']);

        $fee_start = explode(';',$currency['fee_start']);

        $organizers_start = explode(';',$currency['organizers_start']);

        $coorganizers_start = explode(';',$currency['coorganizers_start']);

        $curator_start = explode(';',$currency['curator_start']);

        $artist_start = explode(';',$currency['artist_start']);

        $keyword_start = explode(';',$currency['keyword_start']);

        $type_exhibition_start = explode(';',$currency['type_exhibition_start']);

        $tags = array(
            //展览名
            'name_tags' =>$name_exhibition_start,

            //展览时间：
            'time_tags' => $time_exhibition_start,

            //城市加省份,需要加工处理
            'city_tags' => $city_province_start,

            //地址
            'address_tags' => $address_start,

            //费用
            'fee_tags' => $fee_start,

            //主办单位
            'organizers_tags' => $organizers_start,

            //协办单位
            'coorganizers_tags' => $coorganizers_start,

            //策划
            'curator_tags' => $curator_start,

            //相关艺术家
            'artist_tags' => $artist_start,

            //关键字
            'keyword_tags' => $keyword_start,

            //展览类型
            'type_exhibition_tags' => $type_exhibition_start,
        );

        //print_r($tags);die;

        $tags_magic = array( 'name_tags','time_tags','city_tags','address_tags',
            'fee_tags','organizers_tags','coorganizers_tags','curator_tags',
            'artist_tags','keyword_tags','type_exhibition_tags');

        //去掉标签中的空格
        foreach ($anything_start as $key=>$val){
            $anything_start[$key] = preg_replace('/\s+/','',$anything_start[$key]);
        }
        foreach ($anything_end as $key=>$val){
            $anything_end[$key] = preg_replace('/\s+/','',$anything_end[$key]);
        }
        foreach ($introduction_start as $key=>$val){
            $introduction_start[$key] = preg_replace('/\s+/','',$introduction_start[$key]);
        }
        foreach ($introduction_end as $key=>$val){
            $introduction_end[$key] = preg_replace('/\s+/','',$introduction_end[$key]);
        }
        //anything标签不仅要去掉空格还要加上两种冒号
        //冒号:：
        //$colon = array(':','：');
        foreach($tags as $key=>$val){
            foreach ($tags[$key] as $key_temp=>$val_temp){
                if($val_temp != '') {
                    $tags[$key][$key_temp] = preg_replace('/\s+/', '', $tags[$key][$key_temp]);
                    $tags[$key][] = $tags[$key][$key_temp] . ':';
                    $tags[$key][$key_temp] = $tags[$key][$key_temp] . '：';
                }
            }
        }

        /*echo '<br />anything_start:::';
        print_r($anything_start);
        echo '<br />anything_end:::';
        print_r($anything_end);
        echo '<br />introduction_start:::';
        print_r($introduction_start);
        echo '<br />introduction_end:::';
        print_r($introduction_end);die;*/

        $contents = preg_replace('/\s+/','',$contents);
        $contents_temp = '';
        $start = 0;
        $pos = false;
        while (isset($anything_start[$start])){
            $pos = mb_strpos($contents,$anything_start[$start]);
            if($pos !== false){
                break;
            }
            ++$start;
        }
        if($pos === false){
            cpError::write('url:'.$url.'网页anything_start错误，请改正','详情页分析错误');
        }else{
            --$start;
            $contents_temp = mb_substr($contents,$pos+mb_strlen($anything_start[$start]));

            $start = 0;
            while (isset($anything_end[$start])){
                $pos = mb_strpos($contents_temp,$anything_end[$start]);
                if($pos !== false){
                    break;
                }
                ++$start;
            }

            if($pos === false){
                cpError::write('url:'.$url.'网页anything_end错误，请改正','详情页分析错误');
            }
            //$contents_temp为anything片段
            --$start;
            $contents_temp = mb_substr($contents_temp,0,$pos+mb_strlen($anything_end[$start]));
        }



        $details_temp = array();
        //智能分析anything
        if ($contents_temp != null) {
            //利用$tags_magic得到键值
            //echo $tags[$tags_magic[0]];die;
            for ($i = 0; isset($tags[$tags_magic[$i]])&&isset($tags_magic[$i]); $i++) {
                $temp_temp_temp ='';
                for ($j = 0; isset($tags[$tags_magic[$i]][$j])&&$tags[$tags_magic[$i]][$j]!=''; $j++) {
                    //使用$tags[$i][$j]去对比串
                    $pos = mb_strpos($contents_temp,$tags[$tags_magic[$i]][$j]);

/*                    echo 'tags:';
                    print_r($tags) ;echo '<br />';
                    echo '$tags_magic:';
                    print_r($tags_magic);echo '<br />';die;*/

                    /*echo '$contents_temp:',$contents_temp,'<br />';
                    echo 'tags[$tags_magic[$i]][$j]',$tags[$tags_magic[$i]][$j],'<br />';
                    echo 'pos:',$pos,'<br />';*/

                    if($pos === false){
                        continue;
                    }
                    $temp_temp = mb_substr($contents_temp,$pos+mb_strlen($tags[$tags_magic[$i]][$j]));

                    //echo 'temp_temp:',$temp_temp,'<br />';
                    //echo $detailspage['the_end'];die;
                    if($detailspage['the_end'] != ''){
                        $pos = mb_strpos($temp_temp,$detailspage['the_end']);
                        if($pos !=false){
                            $details_temp[$tags_magic[$i]] = strip_tags(mb_substr($temp_temp,0,$pos));
                            //echo '<br />数据：',$details_temp[$tags_magic[$i]],'<br />';
                            break;
                        }
                    }

                    $pos = mb_strpos($temp_temp,'<');
                    if($pos === false){
                        cpError::write('url:'.$url.'重要错误anything中单项的结束<找不到，请核对网站','详情页分析错误');
                        continue;
                    }
                    $temp_temp_temp = mb_substr($temp_temp,0,$pos);

                    //echo 'temp_temp_temp:',$temp_temp_temp,'<br />';

                    if(mb_strlen($temp_temp_temp) < 3){
                        $pos = mb_strpos($temp_temp,'>');
                        if($pos !== false){
                            $temp_temp = mb_substr($temp_temp,$pos+mb_strlen('>'));
                            $pos = mb_strpos($temp_temp,'<');
                            if($pos === false){
                                cpError::write('url:'.$url.'重要错误anything中单项的结束<找不到，请核对网站','详情页分析错误');
                                $temp_temp_temp = '';
                                continue;
                            }
                            $temp_temp_temp = mb_substr($temp_temp,0,$pos);
                            if(mb_strlen($temp_temp_temp) < 3){
                                $pos = mb_strpos($temp_temp,'>');
                                if($pos !== false){
                                    $temp_temp = mb_substr($temp_temp,$pos+mb_strlen('>'));
                                    $pos = mb_strpos($temp_temp,'<');
                                    if($pos === false){
                                        cpError::write('url:'.$url.'重要错误anything中单项的结束<找不到，请核对网站','详情页分析错误');
                                        $temp_temp_temp = '';
                                        continue;
                                    }
                                    $temp_temp_temp = mb_substr($temp_temp,0,$pos);
                                    if(mb_strlen($temp_temp_temp) < 3){
                                        $pos = mb_strpos($temp_temp,'>');
                                        if($pos !== false){
                                            $temp_temp = mb_substr($temp_temp,$pos +mb_strlen('>'));
                                            $pos = mb_strpos($temp_temp,'<');
                                            $details_temp['address_tags'] = mb_substr($temp_temp,0,$pos);
                                        }



                                    }
                                }
                            }
                        }
                    }
                    //终于得到正确的值
                    //echo 'details:';
                    //print_r($details_temp);
                    //die;
                }
                if($temp_temp_temp != '') {
                    $details_temp[$tags_magic[$i]] = strip_tags($temp_temp_temp);
                    $temp_temp_temp = '';
                }

            }
            //需要二次处理的 展览时间opentimestart,opentimeend；展览城市city,province；
            $details_list['type'] = $details_temp['type_exhibition_tags'];
            $details_list['address'] = $details_temp['address_tags'];
            $details_list['fee'] = $details_temp['fee_tags'];
            $details_list['organizers'] = $details_temp['organizers_tags'];
            $details_list['coorganizers'] = $details_temp['coorganizers_tags'];
            $details_list['curator'] = $details_temp['curator_tags'];
            $details_list['keyword'] = $details_temp['keyword_tags'];
            $details_list['artist'] = $details_temp['artist_tags'];
            $details_list['name'] = $details_temp['name_tags'];

            //print_r($details_temp);die;
            //echo 'temp::',$details_temp['time_tags'],'<br />';
            //$ret = $this->opentimemaker($details_temp['time_tags'],$url);
            /*echo 'ret:::';
            print_r($ret);*/
            //展览时间处理
            $details_list = array_merge($details_list,$this->opentimemaker($details_temp['time_tags'],$url));

            //展览城市处理
            $details_list = array_merge($details_list,$this->citymaker($details_temp['city_tags'],$url));


            //抓取展览介绍
            $start = 0;
            while (isset($introduction_start[$start])){
                $pos = mb_strpos($contents,$introduction_start[$start]);
                if($pos !== false){
                    break;
                }
                ++$start;
            }

            $flag = $start;

            if($pos === false){
                cpError::write('url:'.$url.'网页anything_start错误，请改正','详情页分析错误');
            }else{
                $contents_temp = mb_substr($contents,$pos+mb_strlen($introduction_start[$flag]));
                $pos = mb_strpos($contents_temp,$introduction_end[$flag]);

                if($pos === false){
                    cpError::write('url:'.$url.'网页anything_end错误，请改正','详情页分析错误');
                }
                //$contents_temp为introduction片段
                $contents_temp = mb_substr($contents_temp,0,$pos+mb_strlen($introduction_end[$flag]));
            }

            $details_list['introduction'] = strip_tags($contents_temp);

            //全部数据得到，返回数据
            return $details_list;
        }

        return '';
    }

    //处理展览时间函数，分离开展时间与闭展时间
    //@param $time 类似2017/03/26~2017/04/03
    //@return array('opentimestart' => '2017-03-26','opentimeend' => '2017- 04-03')
    protected function opentimemaker($time,$url){
        if($time == '') {
            return false;
        }
        $ret = array();
        //$opentime = '';
        //$closetime = '';
        //开始结束连接符
        $tags = array(
            ' ',
            '~',
            '～',
            '至',
            '到'
        );
        $pos = '';
        for($i = 0;isset($tags[$i]);$i++){
            $pos = mb_strpos($time,$tags[$i]);
            if($pos !== false){
                break;
            }
        }
        if($pos === false){
            cpError::write('url:'.$url.'发现新的展览时间连接符,请更新','详情页分析错误');
            return false;
        }

        $temp = explode($tags[$i],$time);
        if(isset($temp[4])){
            cpError::write('url:'.$url.'发现新的展览时间规则,请更新','详情页分析错误');
            return false;
        }
        //补全时间
        $opentime = $temp[0];
        if($temp[1] < $temp[0]){
            $closetime = date('Y').$temp[1];
            if($closetime < $opentime){
                $closetime = (date('Y')+1).$temp[1];
            }
        }else{
            $closetime = $temp[1];
        }
        //完全处理
        $ret['opentimestart'] = strtotime($opentime);
        $ret['opentimeend'] = strtotime($closetime);


        return $ret;
    }

    //处理得到最终的省城市
    //@param $city_province 夹杂着城市与省份的字符串
    protected function citymaker($city_province,$url){
        if($city_province == ''){
            return false;
        }
        return judgeCityAndProvince($city_province,$url);
    }

    //找到片段中需要下载的图片，标签使用<img src="或去空格后使用<imgsrc="
    //@return 图片信息列表，用来存储，但是没有展览id，所以无法存储，只能返回
    protected function getImgUrl($contents,$detailspage,$url){

        $details_perfix = $detailspage['details_perfix'];

        $tags = '<img src="';
        $pos = mb_strpos($contents,$tags);
        if($pos === false){
            $tags = '<img href="';
        }
        if($pos === false){
            cpError::write('url:'.$url.'图片标签错误不是<img src="或者<img href="','图片抓取错误');
            return false;
        }
        $imgurl_list = array();
        $imgurl_num = 0;
        $pos = mb_strpos($contents,$tags);

        while ($pos !== false) {
            $contents = mb_substr($contents, $pos + mb_strlen($tags));
            $pos = mb_strpos($contents,'"');
            $imgurl_list[$imgurl_num]['imgurl'] = mb_substr($contents,0,$pos);
            //补全imgurl
            $pos_temp = mb_strpos($imgurl_list[$imgurl_num]['imgurl'],$details_perfix);
            if($pos_temp === false){
                $imgurl_list[$imgurl_num]['imgurl'] = $details_perfix.$imgurl_list[$imgurl_num]['imgurl'];
            }
            //看是否有需要抓取的图片信息
            $pos = mb_strpos($contents,'p>');
            if($pos === false){
                cpError::write('url:'.$url.'图片名字用p找不到，请查证','图片抓取错误');
            }else{
                $contents = mb_substr($contents,$pos+mb_strlen('p>'));
                $pos = mb_strpos($contents,'</p');
                $imgurl_list[$imgurl_num]['name'] = strip_tags(mb_substr($contents,0,$pos));
            }

            $pos = mb_strpos($contents,$tags);
            ++$imgurl_num;
        }


        return $imgurl_list;
    }


    //通用保存图片
    protected function saveImg($img_url){
        $folder = ROOT_PATH.'exhibits/';

        if(!is_dir($folder)){
            mkdir($folder,0777);
            //chmod($folder,0777);
        }
        $folder = $folder.date('Ymd').'/';
        if(!is_dir($folder)){
            mkdir($folder,0777);
            //chmod($folder,0777);
        }


        $pic_format = array('.bmp','.jpg','.tiff','.gif','.pcx','.tga','.exif','.jpeg','.png',
            '.fpx','.svg','.psd','.cdr','.pcd','.dxf','.ufo','.eps','.ai','.raw','.WMF');

        //echo 'saveImage:','<br />','imgurl:',$img_url,'<br />','path:',$folder,'<br />';
        $img_url = str_replace('\\','',$img_url);

        $end = explode(".", $img_url);
        $temp_i = 0;
        while(isset($end[$temp_i])){
            ++$temp_i;
        }
        $end = "." . $end[$temp_i-1];

        if(!in_array($end,$pic_format)){
            return false;
        }

        $header = FormatHeader($img_url);

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //要头信息
        curl_setopt($ch,CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  //设置头信息的地方

        //不要正文
        curl_setopt($ch, CURLOPT_NOBODY, 1);

        curl_setopt($ch,CURLOPT_URL,$img_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_exec($ch);


        $content_type = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);
        curl_close($ch);


        $pos = mb_stripos($content_type,'image');
        if($pos === false){
            $pos = mb_strpos($content_type,'img');
            if($pos === false){
                return '';
            }
        }

        $pic_name = $this->utime() . rand(1111, 9999) . $end;


        //set_time_limit(0); //限制最大的执行时间
        $destination_folder = $folder; //文件下载保存目录
        $newfname = $destination_folder . $pic_name;//文件PATH

        //echo 'newfname:',$newfname,'<br />';

        $ch = curl_init();
        try{$fp = fopen($newfname,'wb');}catch(Exception $err){
            cpError::write($err->getMessage());
            return 0;}
        //echo 'fopen is ok!!';
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  //设置头信息的地方

        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch,CURLOPT_URL,$img_url);
        curl_setopt($ch,CURLOPT_FILE,$fp);

        curl_exec($ch);
        curl_close($ch);
        fclose($fp);

        //生成缩略图
        Image::thumb($newfname,$destination_folder.'64_'.$pic_name,'','640','640',6);


        /*$image = new \Think\Image();
        //echo $newfname;
        try{$image->open($newfname);}catch (Exception $err){return 0;}
        echo 'Thinkphp open is right!!!';
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
        //echo $destination_folder.'64_'.$pic_name;die;
        try{$image->thumb(640, 640)->save($destination_folder.'64_'.$pic_name);}catch (Exception $err){return 0;}
        //echo 'Thinkphp thumb is right!!','<br />';
        //die;*/
        /*$rand = rand(0,30);
        sleep($rand);*/

        return $pic_name;
    }

    protected function utime()
    {
        return time();
    }





















    /************************************************************************************************************************/
/************************************************************************************************************************/
/************************************************************************************************************************/
/************************************************************************************************************************/
/************************************************************************************************************************/
/************************************************************************************************************************/

	//http://auction.artron.net/SHJP-1377/PMH207671/PZ2030453
	/*public function getContentUrl(){
		$url=$_POST['url'];
		$page=$_POST['page'];
        if(!is_numeric($page)){
            exit;
        }
        for($num=0;$num<$page;$num++){
            $nowpage=$num+1;
            $this->createContent($this->getDetailUrl("$url-0-9-$nowpage/"));
        }
        return 'ok';
	}
	protected function createContent($fileUrl){
        if(is_array($fileUrl)){
            $fileUrlList =array();
            foreach($fileUrl as $val){
                $fileUrlList[]=$val;
            }
            if(is_array($fileUrlList)){
                foreach($fileUrlList as $key=>$item){
                    $infoDetail=$this->getInfoDetail($item);
                    $data['author']=$infoDetail['author'];
                    $data['content']=$infoDetail['auction_desc'];
                    $data['pictures']="collectibles/".$this->saveImage($infoDetail['img'],$this->filepath);
                    $auction_time=strtotime($infoDetail['auction_time']);
                    $data['title']=$infoDetail['title'];
                    $data['add_time']=$auction_time;

                    $company=model('companies')->find("apellation='".$infoDetail['company']."'","id","id DESC");
                    if(!$company){
                        $company_id=model('companies')->insert(array('apellation'=>$infoDetail['company']));
                    }else{
                        $company_id=$company['id'];
                    }

                    $expo=model('expo')->find("name='".$infoDetail['expo']."'","id","id DESC");

                    if(!$expo){
                        $expo_id=model('expo')->insert(array('name'=>$infoDetail['expo'],'company'=>$company_id));
                    }else{
                        $expo_id=$expo['id'];
                    }

                    $salesroom=model('salesroom')->find("room_name='".$infoDetail['concert']."'","id","id DESC");

                    if(!$salesroom){
                        $salesroom_id=model('salesroom')->insert(array(
                            'room_name'=>$infoDetail['concert'],
                            'expo_id'=>$expo_id,
                            'auction_starttime'=>$auction_time,
                            'company_id'=>$company_id));
                    }else{
                        $salesroom_id=$salesroom['id'];
                    }
                    $data['lot']=str_replace("*","",$infoDetail['lot']."");;
                    $data['dimension']=$infoDetail['size'];
                    if($infoDetail['currency']!='无'){
                        $data['currency']=$infoDetail['currency'];
                        $data['appraisal']=$infoDetail['price'];
                    }else{
                        $data['appraisal']=$infoDetail['currency'].$infoDetail['price'];
                    }

                    $data['salesroom_id']=$salesroom_id;

                    $data['expo_id']=$expo_id;
                    $data['companies_id']=$company_id;

                    $infoUNInsertID=model('collectibles')->find("expo_id=$expo_id AND companies_id=$salesroom_id AND lot='".$data['lot']."'");
                    echo $data['lot'];
                    if(!$infoUNInsertID){
                        $infoInsertID=model('collectibles')->insert($data);
                        if(!empty($infoInsertID)){//标签
                            $datt['category']=$infoDetail['category'];
                            $datt['sort']=$infoDetail['sort'];
                            $datt['times']=$infoDetail['times'];
                            $datt['log_id']=$infoInsertID;

                            if(is_array($datt)){
                                model("tags")->insert($datt);
                            }
                        }
                    }
                }
            }
        }
        return $infoUNInsertID;
    }
	protected function getDetailUrl($filename){
		$contentUrl=array();
		$baseUrl="http://auction.artron.net/";
		phpQuery::newDocumentFile($filename);
		$artlist = phpQuery::pq(".imgList");
		foreach($artlist as $li){
			$liData=pq($li)->find('li')->html();
			$pattern='/\"\/(.*)\" target=\"_blank\"/';
			preg_match_all($pattern,$liData,$arr,PREG_SET_ORDER);
			foreach($arr as $item){
				$contentUrl[]=$baseUrl.$item[1];
			}
			$arr=array();
		}
		$contentUrl=array_unique($contentUrl);
		return $contentUrl;
	}

	//获取文章大图
	protected function getInfoDetail($filename){
		phpQuery::newDocumentFile($filename);
		//get info content
		$arcInfo = phpQuery::pq(".worksInfo");
		foreach($arcInfo as $li){
			$detailInfoTable=pq($li)->find('table')->html();
			$detailInfoTable=preg_replace("/(\s|\&nbsp\;|　|\xc2\xa0)/", "", strip_tags($detailInfoTable));
			//Author
			$isAuthor=mb_strpos($detailInfoTable,'作者');
			$isSize=mb_strpos($detailInfoTable,'尺寸');
			$detail['author']=mb_substr($detailInfoTable,$isAuthor+6,$isSize-6);//作者
			$detailInfoTable=mb_substr($detailInfoTable,$isSize+6);
//Size
			$isType=mb_strpos($detailInfoTable,'作品分类');
			$detail['size']=mb_substr($detailInfoTable,0,$isType);//尺寸
			$detailInfoTable=mb_substr($detailInfoTable,$isType+12);
//Sort tags
			$isTimes=mb_strpos($detailInfoTable,'创作年代');
			$types=mb_substr($detailInfoTable,0,$isTimes);//作品分类
			$types=explode('&gt;',$types);
			if(count($types)==2){
				$detail['category']=$types[0];
				$detail['sort']=$types[1];
			}
			$detailInfoTable=mb_substr($detailInfoTable,$isTimes+12);
//Times
			$isValue=mb_strpos($detailInfoTable,'估价');
			$detail['times']=mb_substr($detailInfoTable,0,$isValue);//年代
			$detailInfoTable=mb_substr($detailInfoTable,$isValue+6);
//Concert
			$isExpo=mb_strpos($detailInfoTable,'专场');
			$envalue=mb_substr($detailInfoTable,0,$isExpo);
			$detail['currency']=substr($envalue,0,3);//估价
			$isDeal=mb_strpos($detailInfoTable,'成交价');

			$detail['price']=substr($envalue,3,$isDeal+24);//估价
			$detailInfoTable=mb_substr($detailInfoTable,$isExpo+6);
//Auction_Time
			$isAuctionTime=mb_strpos($detailInfoTable,'拍卖时间');
			$detail['concert']=mb_substr($detailInfoTable,0,$isAuctionTime);
			$detailInfoTable=mb_substr($detailInfoTable,$isAuctionTime+12);
//Company
			$isCompany=mb_strpos($detailInfoTable,'拍卖公司');
			$detail['auction_time']=mb_substr($detailInfoTable,0,$isCompany);
			$detailInfoTable=mb_substr($detailInfoTable,$isCompany+12);
//Auction_Show
			$isShow=mb_strpos($detailInfoTable,'拍卖会');
			$detail['company']=mb_substr($detailInfoTable,0,$isShow);
			$detailInfoTable=mb_substr($detailInfoTable,$isShow+9);

			$isExpo=mb_strpos($detailInfoTable,'拍卖会');
			$detail['expo']=mb_substr($detailInfoTable,0,$isExpo+9);

			$detailInfoTable=mb_substr($detailInfoTable,$isExpo+9);
			$isDesc=mb_strpos($detailInfoTable,'说明');
			if(!empty($isDesc)){
				$detail['auction_desc']=mb_substr($detailInfoTable,0,$isDesc);
				$detailInfoTable=mb_substr($detailInfoTable,$isDesc+6);
			}else{
				$detail['auction_desc']='';
			}
//auction_desc
			$isTWOCode=mb_strpos($detailInfoTable,'二维码');
			$detail['auction_desc']=$detail['auction_desc'].mb_substr($detailInfoTable,0,$isTWOCode);
		}
//get image
		$imgurl='';
		$artlist = phpQuery::pq(".imgCont");
		foreach($artlist as $li){
			$liData=pq($li)->find('div')->html();
			$pattern='/data-big=\"(.*)" /';
			preg_match_all($pattern,$liData,$arr,PREG_SET_ORDER);
			$imgurl=$arr[0][1];
			$imgurlarr=explode('"',$imgurl);
			$imgurl=$imgurlarr[0];
		}
		$detail['img']=$imgurl;

//get titile
		$arcTitle=phpQuery::pq(".baseTop");
		foreach($arcTitle as $li){
			$detailInfoTable=pq($li)->find('h1')->html();
			$startPis=strpos($detailInfoTable,' ');
			$detail['title']=substr($detailInfoTable,$startPis+1);
			$lots=explode(" ",$detailInfoTable);
			$detail['lot']=$lots[0];
		}
		return $detail;
	}


	protected function aaasaveImage($img_url,$folder){
		$end=explode(".",$img_url);
		$end=".".$end[3];
		$pic_name=utime().rand(1111,9999).$end;
		//set_time_limit(24*60*60); //限制最大的执行时间
		$destination_folder=$folder?$folder.'/':''; //文件下载保存目录
		$newfname=$destination_folder.$pic_name;//文件PATH
		$file=fopen($img_url,'rb');

		if($file){
			$newf=fopen($newfname,'wb');
			if($newf){
				while(!feof($file)){
					fwrite($newf,fread($file,1024*8),1024*8);
				}
			}
			if($file){
				fclose($file);
			}
			if($newf){
				fclose($newf);
			}
			Image::thumb($newfname,$destination_folder.'64_'.$pic_name,'','640','640',6);
			return $pic_name;
		}
		return false;
	}*/

}